package com.sx.shop.controller;

import com.sx.shop.entity.Shop;
import com.sx.shop.entity.User;
import com.sx.shop.service.ShopService;
import com.sx.shop.vo.ResultVo;
import org.apache.catalina.LifecycleState;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/shop")
public class ShopController {
    // DI 依赖注入
    @Autowired
    private ShopService shopService;
    /* 打开用户主页面*/

    //@RequestMapping(value = "/index" method="get")
    //指定请求方式
    @GetMapping("/index")
    public String openPage() {
        //这是一个html页面
        return "shop";
    }


    /* 获取用户列表数据  若用户id=1则为超级管理员，查询所有商店。否则为当前用户创建的商店*/
    @GetMapping(value = "/list")
    @ResponseBody//只返回数据不找页面
    public ResultVo selectShop(String keyword , Integer page , Integer limit, HttpSession session){
       //User user=(User) session.getAttribute("current");
        User user=(User) SecurityUtils.getSubject().getPrincipal();
        int id=user.getId();
        if (id==1) {
            return shopService.selectShop(keyword, page, limit);
        }else{
            return shopService.selectShopByCreatebyid(keyword,page,limit,id);
        }
    }


    @ResponseBody
    @PostMapping(value="/save")
    public void saveShop(Shop shop)
    {
        shopService.doSave(shop);
    }


    @ResponseBody
    @PostMapping(value="/del")
    public void delShop(@RequestParam(value = "id"/* ,required = false */) Integer shopid) {
        shopService.doDel(shopid);
    }


    @ResponseBody
    @GetMapping(value = "/{id}")
    public Shop selectShopById(@PathVariable("id") Integer id) {

        return 	shopService.selectShopById(id);
    }

    @ResponseBody
    @GetMapping(value = "/top")
    public List<Shop> selectShopByCreatebyid(HttpSession session) {
//        User user=(User) session.getAttribute("current");
        User user=(User)SecurityUtils.getSubject().getPrincipal();
        int createby_id=user.getId();
            return shopService.selectShopByCreatebyid(createby_id);
        }

}
